package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.dtos.inventory.InventoryHisInput;
import com.myf.dtos.inventory.InventoryHisQueryInput;
import com.myf.dtos.inventory.InventoryQueryInput;
import com.myf.features.BaseController;
import com.myf.services.InventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/inventory")
@Tag(name = "库存功能", description = "库存相关API接口")
public class InvetoryController extends BaseController {
    private final InventoryService inventoryService;

    public InvetoryController(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    /// 保存
    @Operation(summary = "保存出入库记录，并计算实时库存")
    @PutMapping("his/save")
    public ApiResult save(@RequestBody InventoryHisInput input) {
        try {
            inventoryService.save(input, getCurrentUser().id);
            return ApiResult.createSuccess(1);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "分页查询出入库列表接口")
    @PostMapping("his/query")
    public ApiResult queryHis(@RequestBody InventoryHisQueryInput input) {
        try {
            return ApiResult.createSuccess(inventoryService.queryHis(input));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "分页查询实时库存列表接口")
    @PostMapping("query")
    public ApiResult query(@RequestBody InventoryQueryInput input) {
        try {
            return ApiResult.createSuccess(inventoryService.query(input));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "根据id获取出入库记录详细信息的接口")
    @GetMapping("his/{id}")
    public ApiResult get(@PathVariable int id) {
        try {
            var data = inventoryService.getById(id);
            return ApiResult.createSuccess(data);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }
}
